package com.example.seh.home.substitution;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seh.R;
import com.example.seh.UserDTO;
import com.example.seh.chat.ChatActivity;
import com.example.seh.constant.Constant;
import com.example.seh.home.SelectActivity;
import com.example.seh.home.pay.PayActivity;
import com.example.seh.util.LoginActivity;
import com.example.seh.util.UserMsg;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class CourierDetailActivity extends AppCompatActivity {
    private TextView content;
    private TextView courierAddress, courierReceiverAddress, courierName, courierPhone, courierPrice, courierDetail, pickupCode;
    private Button substitutionBtn, call;
    private DeliverDTO deliverDTO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courier_detail);
        getViews();
        setListeners();
        getActivityDatas();
    }

    private void getActivityDatas() {
        Intent fromHomeFragment = getIntent();
        deliverDTO = (DeliverDTO) fromHomeFragment.getSerializableExtra("couriers");
        courierAddress.setText(deliverDTO.getAddressName());
        courierReceiverAddress.setText(deliverDTO.getReceiveName());
        courierName.setText(deliverDTO.getUserDTO().getUsername());
        courierPhone.setText(deliverDTO.getUserDTO().getPhone());
        courierPrice.setText(deliverDTO.getDeliverPrice() + "");
        courierDetail.setText(deliverDTO.getDeliverDesc());
    }

    private void setListeners() {
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), SelectActivity.class);
                startActivity(intent);
            }
        });
        substitutionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(CourierDetailActivity.this);
                    builder.setMessage("你确定要代取此快递吗");
                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            OkHttpClient okHttpClient = new OkHttpClient();
                            FormBody body = new FormBody.Builder()
                                    .add("userId", UserMsg.userDTO.getUserId() + "")
                                    .add("deliverId", deliverDTO.getDeliverId() + "")
                                    .build();
                            Request request = new Request.Builder()
                                    .url(Constant.ADDDELIVERORDER)
                                    .post(body)
                                    .build();
                            Log.e("地址：", request.url().toString());
                            Call call = okHttpClient.newCall(request);
                            call.enqueue(new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {
                                    Log.e("okhttp", "获取数据失败");
                                    e.printStackTrace();
                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    String result = response.body().string();
                                    Log.e("deliver", result);

                                    if ("代取成功".equals(result)) {
                                        Intent intent = new Intent(CourierDetailActivity.this, CourierSuccessActivity.class);
                                        intent.putExtra("couriers", deliverDTO);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(getApplicationContext(), "快递已被他人代取", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }

                                }
                            });


                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                } else {
                    Toast.makeText(getApplicationContext(), "请先进行登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(CourierDetailActivity.this, LoginActivity.class);
                    startActivity(intent);
                }


            }
        });

        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin) {
                    UserDTO userDTO = deliverDTO.getUserDTO();
                    Log.e("cour", deliverDTO.toString());
                    Intent intent = new Intent(CourierDetailActivity.this, ChatActivity.class);
                    intent.putExtra("user", userDTO);
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(), "请先进行登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(CourierDetailActivity.this, LoginActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    private void getViews() {
        call = findViewById(R.id.courier_call);
        content = findViewById(R.id.select_courier_detail);
        courierAddress = findViewById(R.id.courier_detail_address);
        courierReceiverAddress = findViewById(R.id.courier_detail_receiver_address);
        courierName = findViewById(R.id.courier_detail_name);
        courierPhone = findViewById(R.id.courier_detail_phone);
        courierPrice = findViewById(R.id.courier_detail_price);
        courierDetail = findViewById(R.id.courier_detail_detail);
//        pickupCode = findViewById(R.id.pickup_code);
        substitutionBtn = findViewById(R.id.substitution);
    }
}